<style type="text/css">
        .layui-table-cell {
            height: auto;
            line-height: 28px;
        }
    </style>
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">会员卡维护</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>会员卡维护</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="user-search-key">
                <option value="">-请选择-</option>
                <option value="card_name">名称</option>
                <option value="service_times">服务次数</option>
            </select>&emsp;
            <input id="user-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="user-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="btn-reset" class="layui-btn icon-btn"><i class="layui-icon">&#x1006;</i>重置</button>
            <button id="shop-btn-add" class="layui-btn icon-btn" ><i class="layui-icon">&#xe654;</i>添加
            </button>
        </div>

        <table class="layui-table" id="card-table" lay-filter="card-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="card-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="cardName-tpl-state">
		<button class="layui-btn layui-btn-xs layui-btn-normal">{{d.cardName}}</button>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="isActive-tpl-state">
	{{# if (d.isActive=== 1) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-normal">有效</button>
	{{# } else if(d.isActive=== 0) { }}  
		<button class="layui-btn layui-btn-xs layui-btn-danger">无效</button>
	{{# } }}
</script>
<script>
    layui.use(['form', 'table', 'util', 'admin', 'element'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var element = layui.element;

        form.render('select');

        // 渲染表格
        
        table.render({
            elem: '#card-table',
            url: 'system/card/list',
            where: {shopId: $("#shopInfo").attr("data")},
            page: true,
            cols: [[
                {type: 'numbers'},
                /* {field: 'cardCode', title: '会员卡编号'}, */
                {field: 'cardName', templet: '#cardName-tpl-state', title: '名称'},
                {field: 'serviceTimes', title: '服务次数'},
                {field: 'isActive',  templet: '#isActive-tpl-state',title: '是否有效'},
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', toolbar: '#card-table-bar', title: '操作'}
            ]]
        });

        // 添加按钮点击事件
        $('#shop-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(card-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
				layer.confirm('确定要删除吗？', function() {
					layer.load(2);
					admin.req("system/card/delete", {
						id : obj.data.id
					}, function(data) {
						layer.closeAll('loading');
						if (data.code == 1) {
							layer.msg(data.msg, {
								icon : 1
							});
							obj.del();
						} else {
							layer.msg(data.msg, {
								icon : 2
							});
						}
					}, "post");
				});
            }
        });

        // 显示表单弹窗
        var showEditModel = function (data) {
            var title = data ? '修改会员卡' : '新增会员卡';
            admin.putTempData('t_card', data);
            var str =  data ? JSON.stringify(data) : " ";
            admin.popupCenter({
            	area: '40%',
            	offset: '10%',
                title: title,
                path: "system/card/editForm",
                data:JSON.stringify(data),
                finish: function () {
                    table.reload('card-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        $('#user-btn-search').click(function () {
            var key = $('#user-search-key').val();
            var value = $('#user-search-value').val();
            table.reload('card-table', {where: {searchKey: key, searchValue: value}});
        });

        // 修改user状态
        form.on('switch(user-tpl-state)', function (obj) {
            layer.load(2);
            $.post('system/user/updateState', {
                userId: obj.elem.value,
                state: obj.elem.checked ? 0 : 1
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 200) {
                    layer.msg(data.msg, {icon: 1});
                    //table.reload('table-user', {});
                } else {
                    layer.msg(data.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            });
        });
        
     // 搜索按钮点击事件
		$('#btn-reset').click(function() {
			location.reload();
		});
    });
</script>